//
//  TLRewardsHeaderView.m
//  WXReader
//
//  Created by huaya on 2025/9/6.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLRewardsHeaderView.h"
#import "TLRewardsHeaderCCell.h"
#import "TLSignTaskDetailModel.h"

@interface TLRewardsHeaderView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *centerV;
@property (weak, nonatomic) IBOutlet UIButton *checkInBtn;

@property (weak, nonatomic) IBOutlet UILabel *bonusLab;
@property (strong, nonatomic) UICollectionView *collectionView;
@end

@implementation TLRewardsHeaderView

+ (instancetype)createView {
    return [[[NSBundle mainBundle] loadNibNamed:@"TLRewardsHeaderView" owner:nil options:nil] lastObject];
}

- (void)setSignTaskVO:(TLSignTaskModel *)signTaskVO{
    if (signTaskVO) {
        _signTaskVO = signTaskVO;
        
        if (self.signTaskVO.todaySigned) {
            
            [self.checkInBtn setBackgroundColor:[UIColor hex_EEEEEE]];
            [self.checkInBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.checkInBtn.userInteractionEnabled = NO;
            
        }else{
            
            [self.checkInBtn setBackgroundColor:[UIColor hex_9E6DFC]];
            [self.checkInBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.checkInBtn.userInteractionEnabled = YES;
            
        }
        
        self.bonusLab.text = [NSString stringWithFormat:@"Bonus %@",TL_UserInfoManager.userInfo.bonusNum];
        
        [self.collectionView reloadData];
    }
}
- (void)awakeFromNib{
    [super awakeFromNib];
    
    self.centerV.layer.cornerRadius = 8;
    self.centerV.backgroundColor = [UIColor whiteColor];
    self.checkInBtn.layer.masksToBounds = YES;
    self.checkInBtn.layer.cornerRadius = 37/2;
    
    [self.centerV addSubview:self.collectionView];
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        
        // 设置布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        // 创建 CollectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(16, 50, SCREEN_WIDTH-32*2, 73) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES; // 启用分页效果
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        // 注册单元格
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"TLRewardsHeaderCCell" bundle:nil] forCellWithReuseIdentifier:@"TLRewardsHeaderCCell"];
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.signTaskVO) {
        return self.signTaskVO.signTaskDetailList.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TLRewardsHeaderCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TLRewardsHeaderCCell" forIndexPath:indexPath];
    if (self.signTaskVO && indexPath.row<self.signTaskVO.signTaskDetailList.count) {
        
        TLSignTaskDetailModel *model = self.signTaskVO.signTaskDetailList[indexPath.row];
        
        cell.model = model;
        
    }
    
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 每个单元格占据整个 CollectionView 的可见区域
    return CGSizeMake((SCREEN_WIDTH-32*2)/7, 73);
}

- (IBAction)checkInAction:(id)sender {
    
    NSNumber *taskId = @(0);
    NSNumber *bonus = @(0);
    
    for (TLSignTaskDetailModel *model in self.signTaskVO.signTaskDetailList) {
        
        if (model.taskStatus.intValue == 0) {
            taskId = model.taskId;
            bonus = model.rewardCoins;
            break;
        }
        
    }
    
    UIView *loadinView = [TL_TopAlertManager showLoading:self];
    MJWeakSelf
    
    [APIManager sendPostRequest:[APIURLManager taskSignIn:taskId.intValue] parameters:@{} model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        [loadinView removeFromSuperview];
        
        if (success) {
            
            MK_BLOCK_EXEC(weakSelf.checkInBlock,bonus);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        
        [loadinView removeFromSuperview];
    }];
    
}

@end
